package com.groupnections.server.database.user;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.groupnections.client.user.UserDatabaseServerCalls;
import com.groupnections.server.database.DatabaseConnection;

/**
 * 
 * @Architecture
 */
@SuppressWarnings("serial")
public class UserDatabaseServerCallsImpl extends RemoteServiceServlet implements
		UserDatabaseServerCalls {

	@Override
	public List<com.groupnections.shared.entities.User> listUsers(String group) {

		PersistenceManager persistenceManager = DatabaseConnection.connect();

		List<com.groupnections.shared.entities.User> users = null;

		users = ListUsers.list(persistenceManager, group);

		persistenceManager.close();

		return users;
	}

	@Override
	public com.groupnections.shared.entities.User getUser(Long userID) {

		PersistenceManager persistenceManager = DatabaseConnection.connect();

		com.groupnections.shared.entities.User user = GetUser.get(
				persistenceManager, userID);

		persistenceManager.close();

		return user;
	}

	@Override
	public void saveUser(com.groupnections.shared.entities.User user,
			String tags) {

		PersistenceManager persistenceManager = DatabaseConnection.connect();

		SaveUser.addNew(persistenceManager, user, tags);

		persistenceManager.close();
	}

	@Override
	public void updateUser(com.groupnections.shared.entities.User user,
			String tags) {

		if (user.getUserID() == null) {

			saveUser(user, tags);

		} else {

			PersistenceManager persistenceManager = DatabaseConnection
					.connect();

			SaveUser.update(persistenceManager, user, tags);

			persistenceManager.close();
		}
	}

	/*
	 * public void deleteProblem(Problem problem) {
	 * 
	 * PersistenceManager persistenceManager = DatabaseConnection.connect();
	 * 
	 * DeleteProblem.delete(persistenceManager, problem);
	 * 
	 * persistenceManager.close(); }
	 */

}
